﻿using DAL;
using Model;
using Smobiler.Core;
using Smobiler.Core.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FubiManger.Pad
{
    partial class PmainForm : Smobiler.Core.Controls.MobileForm
    {
        public PmainForm() : base()
        {
            //This call is required by the SmobilerForm.
            InitializeComponent();
        }
        /// <summary>
        /// 商户ID
        /// </summary>
        public string merchantid { get; set; }
        /// <summary>
        /// 账号ID
        /// </summary>
        public string adminid { get; set; }
        /// <summary>
        /// 账号
        /// </summary>
        public string username { get; set; }
        /// <summary>
        /// 昵称
        /// </summary>
        public string nickname { get; set; }
        /// <summary>
        /// 实时订单
        /// </summary>
        List<ms_order> hourlist = new List<ms_order>();

        private void PmainForm_Load(object sender, EventArgs e)
        {
            //this.Form.DrawerName = "Ordrightcontrol";
            this.Form.Session["nickname"] = nickname;//昵称
            string week = DateTime.Now.DayOfWeek.ToString();
            switch (week)
            {
                case "Monday":
                    week = "星期一";
                    break;
                case "Tuesday":
                    week = "星期二";
                    break;
                case "Wednesday":
                    week = "星期三";
                    break;
                case "Thursday":
                    week = "星期四";
                    break;
                case "Friday":
                    week = "星期五";
                    break;
                case "Saturday":
                    week = "星期六";
                    break;
                case "Sunday":
                    week = "星期日";
                    break;


            }

            lbltime.Text = "欢迎使用福赑收银系统  今天是" + DateTime.Now.Year + "年" + DateTime.Now.Month + "月" + DateTime.Now.Day + "日" + "     " + week;

            hourlist = orderDal.QuerybyHour(merchantid,"2");//实时订单
            Getorder(hourlist);
            //timer1.Start();
        }
        /// <summary>
        /// 获取订单
        /// </summary>
        private void Getorder(List<ms_order> ordlist)
        {
            try
            {
                int count = 0;
                if (panords.Controls.Count > 0)
                    panords.Controls.Clear();
                if (ordlist.Count % 3 == 0)
                    count = ordlist.Count / 3;
                else
                    count = ordlist.Count / 3 + 1;
                panords.Height = count * 128;                
                for (int i = 0; i < ordlist.Count; i++)
                {
                    Pordercontrol con = new Pordercontrol();
                    con.disksn = ordlist[i].orderdisksn;//桌号
                    con.ordsn = ordlist[i].order_sn;//订单号
                    con.ordtime = ordlist[i].add_time;
                    if (!string.IsNullOrEmpty(ordlist[i].user_tel))
                        con.nickname = ordlist[i].user_tel.Substring(0, 3) + "****" + ordlist[i].user_tel.Substring(7, 4);
                    else
                        con.nickname = "现场下单";
                    con.ordid = ordlist[i].id;//编号
                    con.codestatus = ordlist[i].codestatus; //销售状态
                    con.u_sage = ordlist[i].u_sage;//到店或物流
                    panords.Controls.Add(con);
                }

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
        }
        /// <summary>
        /// 订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnorder_Press(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 打开订单详情
        /// </summary>
        /// <param name="ordsn"></param>
        public void Openordinfo(string ordsn)
        {
            // this.Client.Session["ordsn"] = ordsn;
            Ordrightcontrol con = new Ordrightcontrol();
            con.ordsn = ordsn;//订单号
            con.merchantid = merchantid;
            //if (pright.Controls.Count > 0)
            //    pright.Controls.Clear();
            //pright.Width = 300;
            //pright.Controls.Add(con);
            this.Form.ShowDialog(con);
        }
        /// <summary>
        /// 关闭详情
        /// </summary>
        public void Closeinfo(string stype)
        {
            pright.Width = 0;
            if (stype == "1")
            {
                hourlist = orderDal.QuerybyHour(merchantid,"2");//实时订单
                Getorder(hourlist);
            }
            this.Client.RenderFlush();
        }

        private void btnclose_Press(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 下单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnorderfood_Press(object sender, EventArgs e)
        {
            PorderfoodForm frm = new PorderfoodForm();
            frm.merchantid = merchantid;//商家ID
            frm.adminid = adminid;//用户ID
            this.Form.Show(frm, true, (obj, args) =>
            {
                hourlist = orderDal.QuerybyHour(merchantid,"2");//未处理订单
                Getorder(hourlist);
            });
        }
        /// <summary>
        /// 打开侧边栏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pedit_Press(object sender, EventArgs e)
        {

            this.Form.OpenDrawer();//打开侧边栏
        }

        private void PmainForm_KeyDown(object sender, KeyDownEventArgs e)
        {
            if (e.KeyCode == KeyCode.Back)
            {

            }
        }
        int timer = 5;
        private void timer1_Tick(object sender, EventArgs e)
        {
            //timer--;
            //if (timer == 0)
            //{
            //    timer = 5;
            //    hourlist = orderDal.QuerybyHour(merchantid);//未处理订单
            //    Getorder(hourlist);
            //}
        }
        /// <summary>
        /// 核销
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnWrite_off_Press(object sender, EventArgs e)
        {
            Write_offcontrol con = new Write_offcontrol();
            con.merchantid = merchantid;
            this.Form.ShowDialog(con);
        }
        /// <summary>
        /// 刷新订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pmiddle_Refresh(object sender, EventArgs e)
        {
          
        }
        /// <summary>
        /// 刷新订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panmtp_Refresh(object sender, EventArgs e)
        {
            hourlist = orderDal.QuerybyHour(merchantid,"2");//实时订单
            Getorder(hourlist);
        }
    }
}